import { _decorator, Component, director, Node, resources, Sprite, SpriteFrame } from 'cc';
import { GameMgr } from './GameMgr';
import { AudioMgr } from './AudioMgr';
import { UserInfo } from './UserInfo';
const { ccclass, property } = _decorator;

@ccclass('Start')
export class Start extends Component {

    @property(Node)
    setPanel:Node;

    protected start(): void {

        //获取缓存
        if (localStorage.getItem("userInfo")) {
            let userInfo = JSON.parse(localStorage.getItem("userInfo"));
            UserInfo.gateLevel = userInfo.gateLevel;
        }

        if (UserInfo.isAudioBgm) {
            setTimeout(() => {
            AudioMgr.ins.play("audio/bgm0");
            }, 500);
        }
        else{
            AudioMgr.ins.stop();
        }
    }

    onclickStartGameBtn() {
        AudioMgr.ins.playOneShot("audio/click");

        director.loadScene("main")
    }

    onclickSetBtn() {
        AudioMgr.ins.playOneShot("audio/click");

        this.setPanel.active = true;

        this.renewAudioUI();

    }

    onclickSetPanelCloseBtn() {
        AudioMgr.ins.playOneShot("audio/click");

        this.setPanel.active = false;
    }

    onclickSetPanelAudioBtnBtn() {
        AudioMgr.ins.playOneShot("audio/click");

        if (UserInfo.isAudioEffect) {
            UserInfo.isAudioEffect=false;
            
        }
        else{
            UserInfo.isAudioEffect=true;
        }
        this.renewAudioUI();
    }

    onclickSetPanelMusicBtnBtn() {
        AudioMgr.ins.playOneShot("audio/click");

        if (UserInfo.isAudioBgm) {
            AudioMgr.ins.stop();
            UserInfo.isAudioBgm=false;

        }
        else{
            console.log("播放1")

            UserInfo.isAudioBgm=true;
            console.log("播放2")

            AudioMgr.ins.play("audio/bgm0");
            console.log("播放3")
        }
    
        this.renewAudioUI();
    }

    renewAudioUI() {
        if (UserInfo.isAudioBgm) {
            resources.load("audioImg/btn_music_on/spriteFrame", SpriteFrame, (err, res) => {
                this.setPanel.getChildByName("musicBtn").getComponent(Sprite).spriteFrame =res;

            })
        }
        else{
            resources.load("audioImg/btn_music_off/spriteFrame", SpriteFrame, (err, res) => {
                this.setPanel.getChildByName("musicBtn").getComponent(Sprite).spriteFrame =res;

            })
        }

        if (UserInfo.isAudioEffect) {
            resources.load("audioImg/btn_sound_on/spriteFrame", SpriteFrame, (err, res) => {
                this.setPanel.getChildByName("audioBtn").getComponent(Sprite).spriteFrame =res;

            })
        }
        else{
            resources.load("audioImg/btn_sound_off/spriteFrame", SpriteFrame, (err, res) => {
                this.setPanel.getChildByName("audioBtn").getComponent(Sprite).spriteFrame =res;

            })
        }
    }



}


